﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Brilliants.UI.ControlsEx
{
    /// <summary>
    /// NbMessageDialogBox.xaml 的交互逻辑
    /// </summary>
    public partial class NbMessageDialogBox : NbWindow
    {
        public NbMessageDialogBox()
        {
            InitializeComponent();
        }

        public NbMessageBoxDialogResult NbDialogResult { get; set; } = NbMessageBoxDialogResult.Cancel;

        public static NbMessageBoxDialogResult Notity(string message)
        {
            return ShowDialog(message, "提示", NbMessageBoxDialogButtons.Ok, NbMessageBoxDialogButtons.Ok);
        }

        public static NbMessageBoxDialogResult ShowDialog(string message, string CaramePicPath = "")
        {
            return ShowDialog(message, "提示", NbMessageBoxDialogButtons.Ok | NbMessageBoxDialogButtons.Cancel, NbMessageBoxDialogButtons.Ok, "PASS", "NG", CaramePicPath);
        }

        public static NbMessageBoxDialogResult ShowDialog(string message, string title, NbMessageBoxDialogButtons buttons, NbMessageBoxDialogButtons focusButton,
            string OkText = "PASS", string CancelText = "NG", string CaramePic = "")
        {
            var result = NbMessageBoxDialogResult.Cancel;
            Application.Current.Dispatcher.Invoke(() =>
            {
                var box = new NbMessageDialogBox();
                box.MessageLabel.Text = message;
                box.OkButton.Content = OkText;
                box.CancelButton.Content = CancelText;
                box.TitleLabel.Content = title;
                if (!string.IsNullOrEmpty(CaramePic))
                {
                    box.Width = 1000;
                    box.Height = 900;
                    box.CameraPic.Visibility = Visibility.Visible;
                    box.CameraPic.Source = new BitmapImage(new Uri(CaramePic));
                }
                else
                    box.CameraPic.Visibility = Visibility.Collapsed;
                if (Application.Current.MainWindow != null && Application.Current.MainWindow != box)
                {
                    box.Owner = Application.Current.MainWindow;
                    box.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                }
                else
                {
                    box.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                }
                box.CancelButton.Visibility = buttons.HasFlag(NbMessageBoxDialogButtons.Cancel) ? Visibility.Visible : Visibility.Collapsed;
                box.OkButton.Visibility = buttons.HasFlag(NbMessageBoxDialogButtons.Ok) ? Visibility.Visible : Visibility.Collapsed;
                if (focusButton == NbMessageBoxDialogButtons.Ok)
                    box.OkButton.Focus();
                else if (focusButton == NbMessageBoxDialogButtons.Cancel)
                    box.CancelButton.Focus();
                box.ShowDialog();
                result = box.NbDialogResult;
            });
            return result;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.NbDialogResult = NbMessageBoxDialogResult.Cancel;
            this.DialogResult = false;
            this.Close();
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            this.NbDialogResult = NbMessageBoxDialogResult.Ok;
            this.DialogResult = true;
            this.Close();
        }
    }
    public enum NbMessageBoxDialogResult
    {
        Ok = 0,
        Cancel = 1,
    }

    public enum NbMessageBoxDialogButtons
    {
        Ok = 0,
        Cancel = 1,
    }
}
